package org.example.pattern.strategy;

import java.util.HashMap;
import java.util.Map;

public class Result<T> {

    private int code;
    private String msg;
    private T data;
    private Map<Integer, String> errors = new HashMap<>();

    public void putError(int code, String msg) {
        errors.put(code, msg);
    }

    public Map<Integer, String> getErrors() {
        return errors;
    }

    public void setErrors(Map<Integer, String> errors) {
        this.errors = errors;
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(200, "success", data);
    }


    public static <T> Result<T> error(T data) {
        return new Result<T>(-1, "failure", data);
    }

    public static <T> Result<T> result(int code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }

    public static <T> Result<T> result(int code, String msg) {
        return new Result<T>(code, msg);
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", errors=" + errors +
                '}';
    }
}
